/**
 * 
 */
package org.hotpotmaterial.codegenn2.service.impl;

import java.util.Date;

import org.hotpotmaterial.codegenn2.common.HmCodeGenConstants;
import org.hotpotmaterial.codegenn2.common.enums.PersistEnum;
import org.hotpotmaterial.codegenn2.dao.ProjectModuleMapper;
import org.hotpotmaterial.codegenn2.dto.entity.PageDTO;
import org.hotpotmaterial.codegenn2.dto.request.PageRequestDTO;
import org.hotpotmaterial.codegenn2.entity.HmteamAccount;
import org.hotpotmaterial.codegenn2.entity.ProjectModule;
import org.hotpotmaterial.codegenn2.entity.ProjectModuleExample;
import org.hotpotmaterial.codegenn2.service.IProjectModuleService;
import org.hotpotmaterial.codegenn2.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author causer
 *
 */
@Service
public class ProjectModuleServiceImpl implements IProjectModuleService {

	@Autowired
	private ProjectModuleMapper projectModuleMapper;

	@Override
	public PageDTO<ProjectModule> findProjectModules(Long projectId, PageRequestDTO searchDTO) {
		ProjectModuleExample example = new ProjectModuleExample();
		example.createCriteria().andProjectIdEqualTo(projectId).andIsDeletedEqualTo(HmCodeGenConstants.DATA_IS_VALID);
		if (null != searchDTO.getSearchParams() && null != searchDTO.getSearchParams().get("name")) {
			example.getOredCriteria().get(0).andNameLike("%" + (String) searchDTO.getSearchParams().get("name") + "%");
		}
		// 分页查询
		Page<ProjectModule> page = PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize())
				.doSelectPage(() -> projectModuleMapper.selectByExample(example));
		// 构建返回参数
		return PageDTO.<ProjectModule>builder().pageNum(page.getPageNum()).pageSize(page.getPageSize())
				.total(page.getTotal()).data(page.getResult()).build();
	}

	@Override
	@Transactional
	public int insertProjectModule(ProjectModule record) {
		// 默认字段
		setDefaultColumns(record, PersistEnum.insert);
		return projectModuleMapper.insertSelective(record);
	}

	@Override
	public ProjectModule findById(Long id) {
		return projectModuleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional
	public int updateProjectModule(Long id, ProjectModule record) {
		// 默认字段
		record.setId(id);
		setDefaultColumns(record, PersistEnum.update);
		return projectModuleMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional
	public int delProjectModule(Long id) {
		ProjectModule record = projectModuleMapper.selectByPrimaryKey(id);
		if (record != null) {
			setDefaultColumns(record, PersistEnum.delete);
			return projectModuleMapper.updateByPrimaryKeySelective(record);
		}
		return 0;
	}

	/**
	 * 设置默认字段
	 * 
	 * @param record
	 * @param persisttype
	 */
	private void setDefaultColumns(ProjectModule record, PersistEnum persisttype) {
		Date now = new Date();
		// 获取登录信息
		HmteamAccount loginUser = RequestUtil.getAccountInfo();
		switch (persisttype) {
		case insert:
			record.setCreatedAt(now);
			record.setCreatedBy(loginUser.getUserName());
			record.setLastUpdatedBy(loginUser.getUserName());
			record.setLastUpdatedAt(now);
			record.setLastUpdatedTraceid(loginUser.getTraceId());
			record.setIsDeleted(HmCodeGenConstants.DATA_IS_VALID);
		case update:
			record.setLastUpdatedBy(loginUser.getUserName());
			record.setLastUpdatedAt(now);
			record.setLastUpdatedTraceid(loginUser.getTraceId());
		case delete:
			record.setLastUpdatedBy(loginUser.getUserName());
			record.setLastUpdatedAt(now);
			record.setLastUpdatedTraceid(loginUser.getTraceId());
			record.setIsDeleted(HmCodeGenConstants.DATA_IS_INVALID);
		}
	}

}
